import { Brand } from 'src/types'

interface pieData {
  name: string
  value: number
}
export const getPieData = (data: Brand[], status: string) => {
  let newList: pieData[] = []
  let newList2: string[] = []
  if (status === 'all') {
    const salesSum = data.reduce((pre: number, item: Brand) => {
      return pre + (Number(item.offLineSales) + Number(item.onLineSales))
    }, 0)
    const Top5 = data.slice(0, 5)
    const otherSales = data.slice(5).reduce((pre: number, item: Brand) => {
      return pre + (Number(item.offLineSales) + Number(item.onLineSales))
    }, 0)
    newList = Top5.map((item: Brand) => {
      return {
        name: item.name,
        value: Number(item.offLineSales) + Number(item.onLineSales)
      }
    })
    newList.push({
      name: '其他',
      value: otherSales
    })
    newList2 = newList.map((item: pieData) => {
      return `${item.name}  |  ${item.value} ￥${((item.value / salesSum) * 100).toFixed(2)}%`
    })
  }
  if (status === 'on') {
    const salesSum = data.reduce((pre: number, item: Brand) => {
      return pre + Number(item.onLineSales)
    }, 0)
    const Top5 = data.slice(0, 5)
    const otherSales = data.slice(5).reduce((pre: number, item: Brand) => {
      return pre + Number(item.onLineSales)
    }, 0)
    newList = Top5.map((item: Brand) => {
      return {
        name: item.name,
        value: Number(item.onLineSales)
      }
    })
    newList.push({
      name: '其他',
      value: otherSales
    })
    newList2 = newList.map((item: pieData) => {
      return `${item.name}  |  ${item.value} ￥${((item.value / salesSum) * 100).toFixed(2)}%`
    })
  }
  if (status === 'off') {
    const salesSum = data.reduce((pre: number, item: Brand) => {
      return pre + Number(item.offLineSales)
    }, 0)
    const Top5 = data.slice(0, 5)
    const otherSales = data.slice(5).reduce((pre: number, item: Brand) => {
      return pre + Number(item.offLineSales)
    }, 0)
    newList = Top5.map((item: Brand) => {
      return {
        name: item.name,
        value: Number(item.offLineSales)
      }
    })
    newList.push({
      name: '其他',
      value: otherSales
    })
    newList2 = newList.map((item: pieData) => {
      return `${item.name}  |  ${item.value} ￥${((item.value / salesSum) * 100).toFixed(2)}%`
    })
  }
  return { newList, newList2 }
}
